package com.dps.omc.shell;

import com.google.gson.Gson;
import com.keybox.manage.model.SessionOutput;
import com.keybox.manage.util.DBUtils;
import com.keybox.manage.util.SessionOutputUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.websocket.Session;
import java.io.*;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.util.List;

/**
 * Created by lip on 2016-07-13.
 */
public class ShellOutputTask implements Runnable{
    private static Logger log = LoggerFactory.getLogger(ShellOutputTask.class);
    private String charset = "UTF-8"; // 设置编码格式

    private BufferedReader reader;

    private Session session;

    private boolean flag = true;


    public ShellOutputTask(Session session,BufferedReader reader)
    {
        this.session = session;
        this.reader = reader;
    }


    public void run() {

        while (flag) {
            try {
                String last = "";
                String buf2 = "";

//                while ((buf2 = reader.readLine()) != null) {
//                    this.session.getBasicRemote().sendText(buf2);
//                }

                do {
                    this.session.getBasicRemote().sendText(buf2);
                }
                while ((buf2 = reader.readLine()) != null);
                Thread.sleep(1000);
            } catch (IllegalStateException ex) {
                flag = false;
            } catch (Exception ex) {
                log.error(ex.toString(), ex);
            }
        }

    }

}
